package com.kx.smsplantform.controller;

import com.kx.smsplantform.service.CacheService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @Author Sun
 * @Date 2021/7/1 16:01
 */
@RestController
@Api(tags = "缓存模块核心Controller")
@Slf4j
public class CacheController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CacheService cacheService;

    @RequestMapping(value = "/cache/hmset/{key}", method = RequestMethod.POST)
    public boolean hMSet(@PathVariable("key") String key, @RequestBody Map map) {
        log.info("【缓存模块-hmset】 存储数据到hash结构 key={},map={}", key, map);
        cacheService.hMSet(key, map);
        log.info("【缓存模块-hmset】 存储数据到hash结构 操作成功!!!");
        return true;
    }

    @RequestMapping(value = "/cache/hmget/{key}", method = RequestMethod.GET)
    public Map hMGet(@PathVariable("key") String key) {
        log.info("【缓存模块-hmget】 查询hash结构中的数据 key={}", key);
        Map map= cacheService.hMGet(key);
        log.info("【缓存模块-hmget】 查询hash结构中的数据，map={} 操作成功!!!",map);
        return map;
    }

    @GetMapping(value = "/cache/hgetall/{key}")
    public Map hGetAll(@PathVariable String key) {
        log.info("【缓存模块-hGetAll】 查询hash结构全部数据  key = {}", key);
        Map result = cacheService.hGetAll(key);
        log.info("【缓存模块-hGetAll】 成功查询到hash结构全部数据  key = {}，map = {}", key, result);
        return result;
    }

    @GetMapping("/cache/hget/{key}/{field}")
    public Object hGet(@PathVariable String key, @PathVariable String field) {
        log.info("【缓存模块-hGet】 查询hash结构中的value  key = {},field = {}", key, field);
        //因为不用到key查询到的数据类型可能不同，所以使用Object类型接收，谁要获取使用谁自行转换
        Object value = cacheService.hGet(key, field);
        log.info("【缓存模块-hGet】 查询hash结构中的value  key = {},field = {},value = {}", key, field, value);
        return value;
    }

    @PostMapping(value = "/cache/save2cache-persist")
    void saveCache(@RequestParam("key") String key, @RequestParam("value") String value) {
        log.info("【缓存模块-String】 存储数据到String结构  key = {},value = {}", key, value);
        cacheService.saveCache(key, value);
        log.info("【缓存模块-String】 存储数据到String结构 存储成功");
    }

    @GetMapping("/cache/smembers/{key}")
    public Set<Object> smembers(@PathVariable(value = "key") String key) {
        log.info("【缓存模块-Set】 查询Set结构的数据  key = {}", key);
        Set<Object> value = cacheService.smembers(key);
        log.info("【缓存模块-Set】 查询Set结构的数据 查询成功value={}", value);
        return value;
    }


    @RequestMapping(value = "/cache/get/{key}", method = RequestMethod.GET)
    public Object get(@PathVariable("key") String key) {
        log.info("【缓存模块-get】 从String结构获取Value  key = {}", key);
        Object value = cacheService.get(key);
        log.info("【缓存模块-get】 从String结构获取Value  key = {},value = {}", key, value);
        return value;
    }

    // set结构的add操作
    @PostMapping(value = "/cache/sadd/{key}")
    public boolean sAdd(@PathVariable(value = "key") String key, @RequestParam String... set) {
        log.info("【缓存模块-set】 存储数据到set结构  key = {},set{}", key, set);
        boolean value = cacheService.sAdd(key, set);
        log.info("【缓存模块-set】 存储数据到set结构 存储成功");
        return value;
    }

    //查询两个set结构是否有交集
    @GetMapping("/cache/sintersect/{key1}/{key2}")
    Set<Object> sIntersect(@PathVariable(value = "key1") String key1, @PathVariable(value = "key2") String key2) {
        log.info("【缓存模块-set】 查询set结构是否有交集  key1 = {},key2 = {}", key1, key2);
        Set<Object> set = cacheService.sIntersect(key1, key2);
        log.info("【缓存模块-set】 查询set结构脏词为  set={}", set);
        return set;
    }

    //从Zset中查询一定范围的数据
    @GetMapping("/cache/limit/{key}")
    Set limit(@PathVariable String key, @RequestParam Long startTime, @RequestParam Long endTime) {
        return cacheService.limit(key, startTime, endTime);
    }

    //保存数据到Zset中
    @GetMapping("/cache/zsave/{key}")
    void zsave(@PathVariable String key, @RequestParam long time, @RequestParam String randomUUID) {
        cacheService.zsave(key, time, randomUUID);
    }

    //set结构，value为map里面存放多条json数据
    @RequestMapping(value = "/cache/saddmap/{key}", method = RequestMethod.POST)
    void saddMap(@PathVariable String key, @RequestBody Map map) {
        log.info("【缓存模块-set】 存储数据到set结构  key = {},map{}", key, map);
        cacheService.saddMap(key, map);
        log.info("【缓存模块-set】 存储数据到set结构 存储成功");
    }

    //增加
    @PostMapping("/cache/hincr/{key}/{field}/{value}")
    public Long hincr(@PathVariable String key, @PathVariable String field, @PathVariable long value) {
        log.info("【缓存模块-hincr】 增加hash结构中指定filed对应的value  key = {},field = {},value = {}", key, field, value);
        Long result = cacheService.hincr(key, field, value);
        log.info("【缓存模块-hincr】 增加hash结构中指定filed对应的value  key = {},field = {},value = {}", key, field, value);
        return result;
    }

    //在Hash结构中获取所有的key
    @GetMapping(value = "/cache/hgetkeys/{pattern}")
    Set<String> hGetKeys(@PathVariable String pattern){
      return cacheService.hGetKeys(pattern);
    }
}
